package com.green.web.controller.manage;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.green.common.Constant;
import com.green.common.page.Pagination;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.enums.OrgEnum;
import com.green.service.common.ServiceContext;
import com.green.service.manage.AreaPermissionService;
import com.green.service.manage.DataPermissionService;
import com.green.service.manage.OrgService;
import com.green.service.manage.PersonService;
import com.green.service.manage.SyncErpDataService;
import com.green.web.controller.common.BaseController;
import com.green.web.util.PermissionHelper;

/**
 * 人员管理
 * @author 谷辉
 *
 */
@Controller
@RequestMapping(value = "/person/*")
public class PersonController extends BaseController{

	@Resource
	private PersonService personService;
		
	@Resource
	private AreaPermissionService areaPermissionService;
	
	@Resource
	private OrgService orgService;
	@Resource
	private DataPermissionService dataPermissionService;
	@Resource
	private SyncErpDataService syncErpDataService;
	/**
	 * @param modelMap人员列表
	 * @return
	 */
	@RequestMapping(value = "manager")
	public String manager(ModelMap modelMap) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("orgType", OrgEnum.BLOC.name());
		modelMap.put("orgList", this.orgService.getOrgList(param));
		modelMap.put("optCodes", PermissionHelper.getOperateCodes());
		modelMap.put("person", ServiceContext.getLoginPerson());
		return "manage/PersonManager";
	}
	/**
	 * 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	public void listData(Pagination<Person> page,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personName", getString("personName"));
		param.put("isForbidden", getString("isForbidden"));
		param.put("longNumber", ServiceContext.getLoginPerson().getOrg().getLongNumber());		
		page =personService.getPersonPagination(page, param);
        
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	/**
	 * 禁用 启用  用户
	 * 
	 * @return
	 */
	@RequestMapping("deletePerson")
	@ResponseBody
	public Map<String, Object> deletePerson() {
		String id = getString("id");
		int type = getInt("type");
		int count = 0;
		if (null != id && !id.equals("")) {
			Person person=personService.getPersonById(id);
			person.setIsForbidden(type);//禁用
			count=personService.updatePerson(person);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}

	/**
	 * @param modelMap人员信息
	 * @return
	 */
	@RequestMapping(value = "openDetail")
	public String openDetail(ModelMap modelMap) {
		String personId=getString("id");
		Person person=this.personService.getPersonById(personId);
		Org org=orgService.getOrgById(person.getControUnit().getId());
		if(org.getNumber().equals(Constant.JMSYB_NUMBER)){
			modelMap.put("orgType", "JMSYB_NUMBER");
		}else{
			modelMap.put("orgType", "WLSYB_NUMBER");
		}
		modelMap.put("person", person);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personId", personId);		
		modelMap.put("dataName", dataPermissionService.getDataPermissionWmConcat(param));
		modelMap.put("controUnit", org);
		modelMap.put("optCodes", PermissionHelper.getOperateCodes());
		modelMap.put("loginPerson", ServiceContext.getLoginPerson());
		return "manage/PersonDetail";
	}
	
	/**
	 * 保存 模块关系
	 * 
	 * @return
	 */
	@RequestMapping("saveAreaPermission")
	@ResponseBody
	public Map<String, Object> saveAreaPermission() {
		String personId = getString("personId");
		String ids = getString("ids");
		String cityName= getString("cityName");
		if(null!=cityName){
			cityName=cityName.trim();
		}
		int count=this.areaPermissionService.saveAreaPermission(personId,ids,cityName);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 移除 模块关系
	 * 
	 * @return
	 */
	@RequestMapping("deleteAreaPermission")
	@ResponseBody
	public Map<String, Object> deleteAreaPermission() {
		String personId = getString("personId");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personId", personId);
		int count=this.areaPermissionService.deleteAreaPermission(param);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 移除查看权限关系
	 * 
	 * @return
	 */
	@RequestMapping("deleteDataPermission")
	@ResponseBody
	public Map<String, Object> deleteDataPermission() {
		String personId = getString("personId");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personId", personId);
		int count=this.dataPermissionService.deleteDataPermission(param);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	
	/**
	 * 操作是否可以分配板块
	 * 
	 * @return
	 */
	@RequestMapping("isPlateUpdate")
	@ResponseBody
	public Map<String, Object> isPlateUpdate() {
		String personId = getString("personId");
		int isPlate= getInt("isPlate");
		
		Person person=this.personService.getPersonById(personId);
		person.setIsPlate(isPlate);
		int count=this.personService.updatePerson(person);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	
	/**
	 * 同步ERP数据
	 * 
	 * @return
	 */
	@RequestMapping("syncErpDatas")
	@ResponseBody
	public Map<String, Object> syncErpDatas() {
		Map<String, Object> result = new HashMap<String, Object>();
		Person loginPerson = ServiceContext.getLoginPerson();
		if(loginPerson.getNumber().equals("wladmin")){
			syncErpDataService.syncErpDatas(Constant.WLSYB_NUMBER,Constant.NET_ID);
		}else if(loginPerson.getNumber().equals("jmadmin")){
			syncErpDataService.syncErpDatas(Constant.JMSYB_NUMBER,Constant.JOIN_ID);
		}
		result.put("flag", 1);
		return result;
	}
	
	/**
	 * 保存 数据查看权限
	 * 
	 * @return
	 */
	@RequestMapping("saveDataPermission")
	@ResponseBody
	public Map<String, Object> saveDataPermission() {
		String personId = getString("personId");
		String positionId = getString("positionId");
		String orgList = getString("orgList");
		//先删除  在添加
		Map<String, Object> param = new HashMap<String, Object>();
		//根据人员
		if(null!=personId&&!personId.equals(""))
		{
		param.put("personId", personId);
		}
		//根据岗位
		if(null!=positionId&&!positionId.equals(""))
		{
		param.put("positionId", positionId);
		}		
		String dataName= dataPermissionService.getDataPermissionWmConcat(param);

		
		int count =dataPermissionService.saveDataPermission(personId, positionId, orgList);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		result.put("dataName", dataName);
		return result;
	}
}
